﻿using System;
using System.Collections.Concurrent;
using System.Windows.Forms;
using ACSR.SqlServer.Addin.Core.UI;
using ACSR.SqlServer.AddIn.Core.Controllers;
using ACSR.SqlServer.AddIn.Core.Interfaces;

using EnvDTE;
using EnvDTE80;
using ACSR.SSMS.Core;
namespace ACSR.SqlServer.AddIn.Core
{

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class WindowManager
    {
        AddInContext _addInContext;

        private ConcurrentDictionary<object, IToolWindowContext> _windows;

        public WindowManager(AddInContext addInContext)
        {
            this._addInContext = addInContext;
        }
        private readonly string customControlHostingWindowTypeName = "ACSR.SqlServer.Addin.Core.UI.UcControlHost";

        public IToolWindowContext FindOrCreateHostWindow(Control controlToHost, string caption, string guid)
        {
            IToolWindowContext window = FindWindow(customControlHostingWindowTypeName, caption);
            if (window == null)
            {
                return CreateHostWindow(controlToHost, caption, guid);
            }

            return window;
        }

        public IToolWindowContext CreateHostWindow(Control controlToHost, string caption, string guid)
        {
            controlToHost.Dock = System.Windows.Forms.DockStyle.Fill;
            controlToHost.Visible = true;
            var window = CreateCommonUIControl(customControlHostingWindowTypeName, caption, new Guid(guid));
            HostWindow(((dynamic)window.ControlObject), controlToHost);
            return window;
        }

        public void HostWindow(dynamic hostWindow, Control controlToHost)
        {
            hostWindow.HostChildControl(new ControlContext(controlToHost));
        }

        public IToolWindowContext CreateCommonUIControl(string typeName, string caption, Guid guid)
        {
            return CreateToolWindow(this._addInContext.UIDll, typeName, caption, guid);
        }

        public IToolWindowContext CreateToolWindow(string assemblyLocation, string typeName, string Caption, Guid uiTypeGuid)
        {
            Windows2 win2 = _addInContext.ApplicationObject.Windows as Windows2;
            if (win2 != null)
            {
                object controlObject = null;

                Window toolWindow = win2.CreateToolWindow2(_addInContext.AddInInstance, assemblyLocation, typeName, Caption, "{" + uiTypeGuid.ToString() + "}", ref controlObject);
                Window2 toolWindow2 = (Window2)toolWindow;
                toolWindow.Linkable = false;
                //toolWindow.IsFloating = true;
                try
                {
                    toolWindow.WindowState = vsWindowState.vsWindowStateMaximize;
                }
                catch
                {
                }

                toolWindow.Visible = true;

                var ctx = new ToolWindowContext(toolWindow, toolWindow2, controlObject);
                this._windows[new { typeName, Caption }] = ctx;
                return ctx;
            }

            return null;
        }

        public IToolWindowContext FindWindow(string typeName, string Caption)
        {
            IToolWindowContext window = null;
            if (!this._windows.TryGetValue(new { typeName, Caption }, out window))
            {
                return window;
            }

            return null;
        }

        public IToolWindowContext FindOrCreateToolWindow(string typeName, string Caption, Guid uiTypeGuid)
        {
            IToolWindowContext window = FindWindow(typeName, Caption);
            if (window == null)
            {
                return CreateToolWindow(this._addInContext.UIDll, typeName, Caption, uiTypeGuid);
            }

            return window;
        }

        public IToolWindowContext FindOrCreateLogWindow(string name)
        {
            return FindOrCreateToolWindow("ACSR.SqlServer.Addin.Core.UI.UcLogWindow", "ACSR Log", new Guid("3ADC13FF-DCF4-4C49-B2EF-3D78DECDC664"));
        }

        public IToolWindowContext CreateLogWindow(string name)
        {
            return CreateToolWindow(this._addInContext.UIDll, "ACSR.SqlServer.Addin.Core.UI.UcLogWindow", "ACSR Log", new Guid("3ADC13FF-DCF4-4C49-B2EF-3D78DECDC664"));
        }
    }
}
